package api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"net/http"
)

var upGrader = websocket.Upgrader{
	ReadBufferSize:  4096,
	WriteBufferSize: 1024,
	CheckOrigin: func(r *http.Request) bool {
		return true
	},
}

type Connection struct {
	wsConn *websocket.Conn
	//读取websocket的channel
	readChan chan []byte
	//给websocket写消息的channel
	writeChan chan []byte
	closeChan chan byte
}

func WsPing(c *gin.Context) {
	fmt.Println("---------------- WsPing -----------------------")
	wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		return
	}

	conn := &Connection{
		wsConn:    wsConn,
		readChan:  make(chan []byte, 1000),
		writeChan: make(chan []byte, 1000),
		closeChan: make(chan byte),
	}

	go readLoop(conn)
	go writeLoop(conn)
}

func readLoop(conn *Connection) {
	//	var data []byte
	//	var err error
	//	for {
	//		if _, data, err = conn.wsConn.ReadMessage(); err != nil {
	//			goto ERR
	//		}
	//		log.Printf("读取到的值：%s", string(data))
	//	}
	//ERR:
	//	conn.closeChan <- "err"
}

func writeLoop(conn *Connection) {

}


